﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiseLink.RDS.Model.Common;

namespace WiseLink.RDS.Model
{
    public class DeviceRepository :RDSRepositoryBase
    {
        public DeviceRepository() : base()
        {

        }

        public IEnumerable<DeviceViewModel> GetAllDevices()
        {
            var query = from c in ccf20Ent.Devices
                        select new DeviceViewModel
                        {

                            DeviceID = c.DeviceID,
                            CustomerNo = c.Customer.CustomerNo,
                            CustomerName = c.Customer.Name,
                            
                            VehicleModel = c.Vehicle.VehicleType.Manufacture + "-" + 
                                           c.Vehicle.VehicleType.Model + "-" + 
                                           c.Vehicle.VehicleType.EngineType + "-" + 
                                           c.Vehicle.VehicleType.ModelYear,
                            ChassiNo = c.Vehicle.ChassiNo,
                            DeviceSerialNo = c.DeviceKey,
                            DevicePurchaseDate = c.SaleDate,
                            IsRegistered = c.IsRegistered,
                            IsSpecialIDC = c.IsSpecialIDC,
                            IDCNo = c.IDCSimNumber,
                            VehicleLicensePlate = c.Vehicle.VehicleLicense,
                            PrimaryPhone = c.Customer.FirstMobile
                        };
            return query;
        }

        
        public void AddDevice(string deviceSerialNo)
        {
            Device device = new Device();
            device.DeviceKey = deviceSerialNo;
            device.IDCSimNumber = "abc";
            device.InStockDate = DateTime.Now;
            device.IsRegistered = false;
            device.IsSpecialIDC = false;
            device.IsTransfer = false;
            ccf20Ent.Devices.AddObject(device);
            ccf20Ent.SaveChanges();
        }

        public void SellDevice(int deviceid,int customerid)
        {
            Device device = ccf20Ent.Devices.Where(d => d.DeviceID == deviceid).FirstOrDefault();
            if (device != null)
            {
                device.CustomerID = customerid;
                ccf20Ent.SaveChanges();
            }
            
        }

        public void InStockDevice(int deviceid)
        {
            Device device = ccf20Ent.Devices.Where(d => d.DeviceID == deviceid).FirstOrDefault();
            if (device != null)
            {
                device.CustomerID = null;
                ccf20Ent.SaveChanges();
            }
        }

        public void RegisterDevice(int deviceid)
        {
            Device device = ccf20Ent.Devices.Where(d => d.DeviceID == deviceid).FirstOrDefault();
            if (device != null)
            {
                if (device.IsRegistered != null)
                {
                    device.IsRegistered = !device.IsRegistered;
                }
                else
                {
                    device.IsRegistered = true;
                }
                ccf20Ent.SaveChanges();
            }
        }
    }
}
